import QtQuick 1.1
import com.nokia.meego 1.0

Dialog {
    id: valueDialog

    property alias titleStr: titleText.text
    property alias stepSize: valueSlider.stepSize
    property alias minimumValue: valueSlider.minimumValue
    property alias maximumValue: valueSlider.maximumValue
    property alias value: valueSlider.value
    property alias inputMethodHints: textField.inputMethodHints
    property int precision: 0

    function toFixed(value) {
        var number = new Number(value)
        var result = number.toFixed(valueDialog.precision)
//        console.log("toFixed, value",value,
//                    "result", result,
//                    "precision", valueDialog.precision)
        return result
    }

    function updateValue(value) {
        console.log("updateValue")

        console.log("set textfield",
                    "textField.text", textField.text,
                    "toFixed(value)",toFixed(value),
                    "toFixed(parseFloat(textField.text)",
                    toFixed(parseFloat(textField.text)))
        //when value is 0, do nothing
        if (textField.text.length === 0 && toFixed(value) === toFixed(0)) {
            console.log("text value is the same do nothing")
        }
        else if (toFixed(parseFloat(textField.text)) !== toFixed(value)) {
            textField.text = toFixed(value) === 0 ? 0 : toFixed(value)
            console.log("set text value to", textField.text)
        }

        if (toFixed(valueSlider.value) !== toFixed(value)) {
            valueSlider.value = toFixed(value)
            console.log("set valueSlider to ",valueSlider.value)
        }

    }
//    onValueChanged: {
//        updateValue(valueDialog.value, valueDialog)
//    }

    title: Rectangle {
        id: titleField
        height: 80
        width: parent.width
        color: "transparent"
        Text {
            id: titleText
            width: parent.width
            height: parent.height
            horizontalAlignment: Text.AlignHCenter
            anchors.centerIn: parent
            color: "white"
            font.pixelSize: 30
        }
    }

    content: Column {
        width: parent.width
        TextField {
            id: textField
            anchors.horizontalCenter: parent.horizontalCenter
            //            inputMask: "0-9."
            inputMethodHints: Qt.ImhDigitsOnly
            onTextChanged: {
                console.log("textField changed:",text,
                            "fixed", toFixed(parseFloat(text)),
                            "value", toFixed(valueDialog.value))
                if (text.length > 0) {
                    console.log("set valueDialog.value to:",toFixed(parseFloat(text)))
                    updateValue(toFixed(parseFloat(text)))
                }
                else {
                    console.log("set valueDialog.value to 0")
                    updateValue(0)
                }
            }
        }

        Row {
            width: parent.width
            spacing: 2
            Label {
                id: minText
                anchors.verticalCenter: parent.verticalCenter
                text: valueDialog.minimumValue
                color: "white"
                font.pixelSize: 20
            }

            Slider {
                id: valueSlider
                anchors.verticalCenter: parent.verticalCenter
                orientation: Qt.Horizontal
                valueIndicatorVisible: true
                stepSize: 1
                width: parent.width - minText.width - maxText.width
                onValueChanged: {
                    console.log("valueSlider changed:",value,
                                "fixed", toFixed(value),
                                "text value", textField.text)
                    updateValue(value)
                }
            }

            Label {
                id: maxText
                anchors.verticalCenter: parent.verticalCenter
                text: valueDialog.maximumValue
                color: "white"
                font.pixelSize: 20
            }
        }
    }

    buttons: ButtonRow {
        platformStyle: ButtonStyle { }
        anchors.horizontalCenter: parent.horizontalCenter
        Button {
            text: qsTr("OK")
            onClicked: {
                valueDialog.accept()
            }
        }
        Button {
            text: qsTr("Cancel")
            onClicked: valueDialog.reject()
        }
    }
}
